<?php
namespace app\common\lib\model_operate;

/**
 * 工具类
 */
class Tools
{
    /**
     * 处理表信息获取数据模型对象 eg: cmf_member / cmf_member_detail
     * @param $tableName 表名
     * @return 模型对象
     */
    public static function tableToModel($tableName = '')
    {
        $tableName      = cmf_parse_name($tableName);
        $prefix         = self::getPrefix();
        $moduleName     = self::getModuleNameByTableName($tableName);
        $tableModelName = cmf_parse_name(str_replace($prefix, "", $tableName), 1) . "Model";
        $class          = "\\plugins\\$moduleName\\model\\$tableModelName";
        if (!class_exists($class)) {
            return null;
        }

        return (new $class);
    }

    /**
     * 从表名中获取模块名字 cmf_module_config_kkkk
     * @param $tableName 表名字
     * @return 存在返回C语言风格(下划线)的模块标识 , 否则返回空字符串 ""
     */
    public static function getModuleNameByTableName($tableName = '')
    {
        $prefix     = self::getPrefix();
        $tableName  = str_replace("$prefix", "", $tableName);
        $splitArr   = explode('_', $tableName);
        $moduleName = "";
        $tmp        = "";
        foreach ($splitArr as $name) {
            if ($tmp == "") {
                $tmp = $name;
            } else {
                $tmp .= "_$name";
            }
            if (self::checkModuleIsExist($tmp)) {
                $moduleName = $tmp;
            }
        }
        return $moduleName;
    }

    /**
     * 检查模块是否存在
     * @param $moduleName 模块名字
     * @return true|false
     */
    public static function checkModuleIsExist($moduleName)
    {
        $pos        = strpos($moduleName, "plugins/");
        $moduleName = (false === $pos) ? "plugins\\" . cmf_parse_name($moduleName) : cmf_parse_name($moduleName);
        $class      = "\\$moduleName\\controller\\AdminIndexController";
        return class_exists($class);
    }

    /**
     * 获取表前缀
     */
    public static function getPrefix()
    {
        return Config('database.prefix');
    }

    /**
     * 格式化表名:将字符串转换为表名
     * @param $str 需要转换的字符串
     * @param $type 转换类型 1.带前缀 0.不带前缀
     */
    public static function tableNameFormat($str, $type = 0)
    {
        if (!is_string($str) || !in_array($type, [0, 1])) {
            return '';
        }

        $str    = cmf_parse_name($str); //转换为c风格
        $prefix = self::getPrefix();
        $name   = str_replace($prefix, '', $str);
        $name   = str_replace("Model", '', $name);
        if ($type == 1) {
            $name = "$prefix$name";
        }
        return $name;
    }

    /**
     * 获取查询方法
     */
    public static function getQueryMethod()
    {
        $path    = APP_PATH . 'common' . DS . 'lib' . DS . 'model_operate' . DS . 'query';
        $dir     = scandir($path);
        $methods = [];
        foreach ($dir as $key => $value) {
            if ($value == '.' || $value == '..') {
                continue;
            }

            $name  = str_replace('.php', '', $value);
            $class = "\\app\\common\\lib\\model_operate\\query\\" . cmf_parse_name($name, 1);
            if (class_exists($class)) {
                $methods[$name] = (new $class(null))->getFlags();
            }
        }
        return $methods;
    }
}
